package com.game.comment.exection;

import com.game.comment.core.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public AjaxResult serviceException(ServiceException e) {
        String message = e.getMessage();
        log.error("服务异常:{}",message);
        // e.printStackTrace();
        return AjaxResult.fail(message);
    }

    @ExceptionHandler(PlannerException.class)
    @ResponseBody
    public AjaxResult PlannerException(PlannerException e) {
        String message = e.getMessage();
        log.error("配表异常{}",message);
        return new AjaxResult(-20,message);
    }


    @ExceptionHandler(InfoTips.class)
    @ResponseBody
    public AjaxResult InfoException(InfoTips e) {
        String message = e.getMessage();
        log.error("消息已捕获{}",message);
        return AjaxResult.error(message);
    }

    @ExceptionHandler({MethodArgumentNotValidException.class})
    @ResponseBody
    public AjaxResult MethodArgumentNotValidException(MethodArgumentNotValidException e) {
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        String message = allErrors.stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining(";"));
        log.error("异常已捕获");
        e.printStackTrace();
        return AjaxResult.error("请求参数错误: " + message);
    }

    @ExceptionHandler({ConstraintViolationException.class})
    @ResponseBody
    public AjaxResult ConstraintViolationException(ConstraintViolationException e) {
        String message = e.getMessage();//.split(":")[1]
        log.error("异常已捕获");
        e.printStackTrace();
        return AjaxResult.error("请求参数错误: " + message);
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public AjaxResult exception(Exception e) {
        log.error("异常已捕获");
        e.printStackTrace();
        return AjaxResult.error("服务器异常");
    }

    @ExceptionHandler(IndexOutOfBoundsException.class)
    @ResponseBody
    public AjaxResult duplicateKeyException(IndexOutOfBoundsException e) {
        log.error("异常已捕获");
        e.printStackTrace();
        return AjaxResult.error("数组越界");
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseBody
    public AjaxResult HttpMessageNotReadableException(HttpMessageNotReadableException e) {
        log.error("异常已捕获");
        e.printStackTrace();
        return AjaxResult.error("http消息不可读");
    }

    // SerializationException
    // 序列化异常
}
